<?php
require_once 'CMS/Util.php';
require_once 'CMS/Pagination.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/Language.php';
require_once 'CMS/User.php';
/*
Người tạo	: Lại Ngọc Thái Châu - Team 0
Module 	: Comment Product
Chức năng	: Show + Post Comment
 */

class QuestionAnswer_IndexController {
	private $pagination;
	public function init(){
		parent::init();

	}

	public function indexAction()
	{
		$this->pagination = new Pagination();	
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/questionanswer');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
		
		/*========== Start Get Text ==============*/
		$this->view->text_Question		= $language->get('text_Question');
		$this->view->text_Content		= $language->get('text_Content');
		$this->view->text_Comment		= $language->get('text_Comment');
		$this->view->confirm		= $language->get('confirm');
		$this->view->fail		= $language->get('fail');
		$this->view->success		= $language->get('success');
		$this->view->text_Save		= $language->get('text_Save');
		$this->view->text_Edit	= $language->get('text_Image');
		$this->view->textError1 	= $language->get('error1');
		$this->view->textError2 	= $language->get('error2');
		$this->view->textError3 	= $language->get('error3');
		$this->view->textError4 	= $language->get('error4');
		$this->view->textError5 	= $language->get('error5');
		$this->view->textError6 	= $language->get('error6');
		$this->view->textError7 	= $language->get('error6');
		$this->view->text_like			= $language->get('text_like');
		$this->view->text_views			= $language->get('text_views');
		$this->view->text_answer		= $language->get('text_answer');
		$this->view->text_post			= $language->get('text_post');
		$this->view->lag 	= $language->get('lag');
		/*========== End Text ==============*/	
		
		//Start------Check Input Parameter
		$path = APPLICATION_PATH."/widgets/questionanswer/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		//End--------Check Input Parameter
		
		/************ Start check user login *********/
		$session1 = new Zend_Session_Namespace('login.auth');
		if(isset($session1->id_user))
		{
			$this->view->login = "1";
			/************ Start check permission *********/
			$user = new User();
			$user->init();
			$hasmodify = $user->modifiy_content($xmlconfig->name);
			$this->view->permission = $hasmodify;
			/************ End check user permission *********/
		}
		else 
		{
			
			$this->view->login = "0";
		}
		/************ End check user login *********/
		
		$this->pagination = new Pagination();
		
		//Start------Khai báo các parameter
		$tbThings = new QuestionAnswer_Model_Thing();
		$tbUsers = new QuestionAnswer_Model_Users();	
		$tbLike=new QuestionAnswer_Model_Like();
		$tbComment=new QuestionAnswer_Model_Comment();
		$dateInt = new Util();
		$access = true;
		//End--------Khai báo các parameter
		
		if (isset($_GET["id"]) && $dateInt->is_interger($_GET["id"]))
    	{
			$questions = $tbThings->getThingById($_GET["id"]);
			if (count($questions) > 0)
			{
				$question = $questions[0];
				$user = $tbUsers->selectUser($question['user_id']);
				if (!count($user) > 0)
				{
					$access = false;
				}
			}
			else
			{
				$access = false;
			}
		}
		else
		{
			$access = false;
		}
		
		if (count($questions) > 0)
		{			
		$question = $questions[0];
		$question["update_date"] = $dateInt->makeDays($question["update_date"]);
		$decription_lang=unserialize($question["description"]);
		$title_lang=unserialize($question["title"]);

		if (isset($decription_lang[$name['name']]))
			$questions[0]["description"]=$decription_lang[$name['name']];
		else
			$questions[0]["description"]="-----------------------";
		
		if (isset($title_lang[$name['name']]))
			$questions[0]["title"]=$title_lang[$name['name']];
		else
			$questions[0]["title"]="...";
				
		//-----------------------Start update view of things------------------------
		$questions[0]['views']=$questions[0]['views']+1;
		$tbThings->updateViewThing($_GET["id"],$questions[0]['views']);
		$CountLike=count($tbLike->getLikeByThingsId($_GET["id"]));
		$CountComment=count($tbComment->getCommentByThingsId($_GET["id"]));
		//-----------------------end update view of things------------------------//
		
		//Start------Truyền parameter qua View
		$this->view->question = $questions[0];			
		$this->view->user = $user[0];
		$this->view->CountLike = $CountLike;
		$this->view->CountComment = $CountComment;
		//End--------Truyền parameter qua View
		}
	}	
}
?>